<?php
/**
 * DESCRIPTION:
 *  different captcha generators and methods to work with
 * METHODS:
 *  boolean connect($host='localhost', $port='3306', $user='root', $password='', $db='php_muscls') makes a connection
 * ======
 * @author at8eqeq3
 * @version 0.1
 */

function captcha_guess(){
    $questions = array("Ph'nglui mglw'nafh ***~R'lyeh wgah'nagl fhtagn"); //use ~ to split lines
    
    $answers = array("cthulhu");
    $no = mt_rand(0, count($questions)-1);
    $q = $questions[$no];
    $a = $answers[$no];
    captcha_generate($q, $a);
}

function captcha_music(){ //will be better, i think
    //i need some time to think about
}

//the main function, creates and sends image
function captcha_generate($q, $a){
    $im = imagecreate(200, 75);
    $b = imagecolorallocate($im, 10, 10, 10);
    $w = imagecolorallocate($im, 90, 90, 90);
    imagefill($im, 1, 1, $w);
    $s = explode('~', $q);
    $i = 1;
    foreach($s as $k => $v){
        imagestring($im, 2, 2, 10*$i, $v, $b);
        $i++;
    }
    $_SESSION['cc'] = md5($a."abracadabra"); //maybe better use some var from config instead of my 'abra...'
    //setcookie('cc', md5($a."abracadabra"));
    Header("Content-type: image/png");
    imagepng($im);
}

function captcha_check($captcha){
    $control = $_SESSION['cc'];
    unset($_SESSION['cc']);
    if($control == md5($captcha.'abracadabra')){
        return true;
    }else{
        return false;
    }
}

captcha_guess(); //write here a captcha-generating method call

?>